package com.mszlu.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.blog.common.Result;
import com.mszlu.blog.common.aop.Cache;
import com.mszlu.blog.common.aop.ClearCache;
import com.mszlu.blog.common.aop.LogAnnotation;
import com.mszlu.blog.entity.dto.ArticleDto;
import com.mszlu.blog.entity.request.ArticlePage;
import com.mszlu.blog.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Slf4j
@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/abc")
    public Result abc(){
        ArticlePage articlePage = new ArticlePage();
        return articleService.getPage2(articlePage);
    }

    @PostMapping
    //加上此注解代表要对此接口记录日志
    @LogAnnotation(module="文章", operator="获取文章列表")
    @Cache
    public Result<List> page(@RequestBody ArticlePage articlePage){

        return articleService.getPage2(articlePage);
    }

    @PostMapping("/hot")
    @Cache
    public Result hot(){

        return articleService.hot();
    }

    @PostMapping("/new")
    @Cache
    public Result newArticles(){

        return articleService.newArticles();
    }

    @PostMapping("/listArchives")
    @Cache
    public Result listArchives(){

        return articleService.listArchives();
    }

    /**
     * 根据id获取单个文章信息b
     * 并查询到文章的详细信息
     * 组装成articledto
     *
     * @param id
     * @return
     */
    @PostMapping("/view/{id}")
    @Cache
    public Result viewId(@PathVariable Long id){

        Result result = articleService.viewsById(id);

        return result;
    }

    @PostMapping("/publish")
    @ClearCache
    public Result publish(@RequestBody ArticleDto articleDto) {

        return articleService.pulish(articleDto);
    }
}
